package com.football.springboot.controller;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.football.springboot.common.Result;
import com.football.springboot.common.Return;
import com.football.springboot.entity.*;
import com.football.springboot.service.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import jodd.util.StringUtil;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.*;


@Api(tags = "排名榜单管理")
@RestController
@RequestMapping("/rank")
public class RankController {
    @Resource
    private ITeamDataService teamDataService;

    @Resource
    private ITeamService teamService;

    @Resource
    private IPlayerService playerService;

    @Resource
    private IPlayerDataService playerDataService;

    @Resource
    private ILeagueService leagueService;

    @Resource
    private ITeamAbilityService teamAbilityService;

    @Resource
    private IPlayerAbilityService playerAbilityService;

    //前台联赛积分榜列表加载(注意区分主客场,总体的要把主客场加起来)
    @ApiOperation("前台联赛积分榜实现")
    @GetMapping("/front/leagueTable")
    public Result leagueTable(@RequestParam Integer leagueId,
                              @RequestParam String homeAway) {

        QueryWrapper<TeamData> queryWrapperhome = new QueryWrapper<>();
        QueryWrapper<TeamData> queryWrapperaway = new QueryWrapper<>();

        //先分别找出所有主客场数据，然后将相同联赛的相同球队的主客场数据相加
        queryWrapperhome.like("home_away","主场");
        queryWrapperaway.like("home_away","客场");

        queryWrapperhome.eq(leagueId!=null,"league_id",leagueId);
        queryWrapperaway.eq(leagueId!=null,"league_id",leagueId);

        queryWrapperhome.orderByDesc("total_points","total_goal");
        queryWrapperaway.orderByDesc("total_points","total_goal");

        List<TeamData> hometeamdatalist = teamDataService.list(queryWrapperhome);

        List<TeamData> awayteamdatalist = teamDataService.list(queryWrapperaway);

        //先添加其他属性字段
        hometeamdatalist.forEach(hometeamdata->{
            hometeamdata.setTeam(teamService.getById(hometeamdata.getTeamId()));
            teamDataService.countleague(hometeamdata);
        });
        awayteamdatalist.forEach(awayteamdata->{
            awayteamdata.setTeam(teamService.getById(awayteamdata.getTeamId()));
            teamDataService.countleague(awayteamdata);
        });

        System.err.println(homeAway);
        //主客场相加后的总数据
        List<TeamData> allteamdatalist = new ArrayList<>();


            if (homeAway.equals("全部") || homeAway.equals("")){
                //循环比较后插入
                //有主场数据时，查找客场数据，如果有则相加，没有则不加
                hometeamdatalist.forEach(hometeamdata->{
                    TeamData awayteamdata = teamDataService.getupdategamedata(hometeamdata.getTeamId(),hometeamdata.getLeagueId(),"客场");
                    if (!ObjectUtils.isEmpty(awayteamdata)){//存在客场数据
                        System.err.println(hometeamdata);
                        System.err.println(awayteamdata);
                        //执行相加
                        TeamData teamDataall = teamDataService.teamDataall(hometeamdata,awayteamdata);
                        System.err.println(teamDataall);
                        allteamdatalist.add(teamDataall);
                    }else{//不存在客场数据 那只有主场数据
                        allteamdatalist.add(hometeamdata);
                    }

                    allteamdatalist.forEach(teamDataall->{
                        teamDataall.setTeam(teamService.getById(teamDataall.getTeamId()));
                        System.err.println(teamDataall);
                    });
                });
                //有客场数据时，查找主场数据，没有就不加，如果有也不加，因为上面已经加过了
                awayteamdatalist.forEach(awayteamdata->{
                    TeamData hometeamdata = teamDataService.getupdategamedata(awayteamdata.getTeamId(),awayteamdata.getLeagueId(),"主场");
                    if (ObjectUtils.isEmpty(hometeamdata)) {//不存在主场数据 那只有客场数据
                        allteamdatalist.add(awayteamdata);
                    }
                    allteamdatalist.forEach(teamDataall->{
                        teamDataall.setTeam(teamService.getById(teamDataall.getTeamId()));
                        System.err.println(teamDataall);
                    });

                });
                //根据积分、胜场、平场、负场、净胜球、进球 排序
                allteamdatalist.sort(Comparator.comparing(TeamData::getTotalPoints)
                        .thenComparing(TeamData::getTotalWin)
                        .thenComparing(TeamData::getTotalFlatten)
                        .thenComparing(TeamData::getGoalDiff)
                        .thenComparing(TeamData::getTotalGoal).reversed());

                return Result.success(allteamdatalist);

            }else if(homeAway.equals("主场")){
                hometeamdatalist.sort(Comparator.comparing(TeamData::getTotalPoints)
                        .thenComparing(TeamData::getTotalWin)
                        .thenComparing(TeamData::getTotalFlatten)
                        .thenComparing(TeamData::getGoalDiff)
                        .thenComparing(TeamData::getTotalGoal).reversed());
                System.err.println(hometeamdatalist);
                return Result.success(hometeamdatalist);
            }else if (homeAway.equals("客场")){
                awayteamdatalist.sort(Comparator.comparing(TeamData::getTotalPoints)
                        .thenComparing(TeamData::getTotalWin)
                        .thenComparing(TeamData::getTotalFlatten)
                        .thenComparing(TeamData::getGoalDiff)
                        .thenComparing(TeamData::getTotalGoal).reversed());
                System.err.println(awayteamdatalist);
                return Result.success(awayteamdatalist);
            }

        return Result.success(allteamdatalist);
    }


    //前台球队榜单列表加载
    @ApiOperation("前台球队榜单列表加载")
    @GetMapping("/front/teamRank")
    public Result teamRank(@RequestParam Integer leagueId,
                           @RequestParam Integer teamRankId,
                           @RequestParam String homeAway){
        QueryWrapper<TeamData> queryWrapperhome = new QueryWrapper<>();
        QueryWrapper<TeamData> queryWrapperaway = new QueryWrapper<>();

        //先分别找出所有主客场数据，然后将相同联赛的相同球队的主客场数据相加
        queryWrapperhome.like("home_away","主场");
        queryWrapperaway.like("home_away","客场");

        queryWrapperhome.eq(leagueId!=null,"league_id",leagueId);
        queryWrapperaway.eq(leagueId!=null,"league_id",leagueId);

        queryWrapperhome.orderByDesc("total_points","total_goal");
        queryWrapperaway.orderByDesc("total_points","total_goal");

        List<TeamData> hometeamdatalist = teamDataService.list(queryWrapperhome);

        List<TeamData> awayteamdatalist = teamDataService.list(queryWrapperaway);

        //先添加其他属性字段
        hometeamdatalist.forEach(hometeamdata->{
            hometeamdata.setTeam(teamService.getById(hometeamdata.getTeamId()));
            teamDataService.countleague(hometeamdata);
        });
        awayteamdatalist.forEach(awayteamdata->{
            awayteamdata.setTeam(teamService.getById(awayteamdata.getTeamId()));
            teamDataService.countleague(awayteamdata);
        });

        System.err.println(homeAway);
        //主客场相加后的总数据
        List<TeamData> allteamdatalist = new ArrayList<>();

            if (homeAway.equals("全部") || homeAway.equals("")){
                //循环比较后插入

                //有主场数据时，查找客场数据，如果有则相加，没有则不加
                hometeamdatalist.forEach(hometeamdata->{
                    TeamData awayteamdata = teamDataService.getupdategamedata(hometeamdata.getTeamId(),hometeamdata.getLeagueId(),"客场");
                    if (!ObjectUtils.isEmpty(awayteamdata)){//存在客场数据
                        System.err.println(hometeamdata);
                        System.err.println(awayteamdata);
                        //执行相加
                        TeamData teamDataall = teamDataService.teamDataall(hometeamdata,awayteamdata);
                        System.err.println(teamDataall);
                        allteamdatalist.add(teamDataall);
                    }else{//不存在客场数据 那只有主场数据
                        allteamdatalist.add(hometeamdata);
                    }

                    allteamdatalist.forEach(teamDataall->{
                        teamDataall.setTeam(teamService.getById(teamDataall.getTeamId()));
                        System.err.println(teamDataall);
                    });
                });
                //有客场数据时，查找客场数据，没有就不加，如果有也不加，因为上面已经加过了
                awayteamdatalist.forEach(awayteamdata->{
                    TeamData hometeamdata = teamDataService.getupdategamedata(awayteamdata.getTeamId(),awayteamdata.getLeagueId(),"主场");
                    if (ObjectUtils.isEmpty(hometeamdata)) {//不存在主场数据 那只有客场数据
                        allteamdatalist.add(awayteamdata);
                    }
                    allteamdatalist.forEach(teamDataall->{
                        teamDataall.setTeam(teamService.getById(teamDataall.getTeamId()));
                        System.err.println(teamDataall);
                    });

                });
                if (teamRankId==1){
                    //球队进球榜
                    allteamdatalist.sort(Comparator.comparing(TeamData::getTotalGoal)
                                        .thenComparing(TeamData::getAverageGoal).reversed());
                }else if(teamRankId==2){
                    //球队失球榜
                    allteamdatalist.sort(Comparator.comparing(TeamData::getTotalFumble)
                                        .thenComparing(TeamData::getAverageFumble).reversed());
                }else if (teamRankId==3){
                    //球队身价榜
                    allteamdatalist.sort(Comparator.nullsFirst(Comparator.comparing(teamData -> teamData.getTeam().getTeamValue(),(s1,s2)->{
                        return s2.compareTo(s1);
                    })));
                }else if (teamRankId==4){
                    //球队控球榜
                    allteamdatalist.sort(Comparator.comparing(TeamData::getTotalControl).reversed());
                } else if (teamRankId==5){
                    //球队射门榜
                    allteamdatalist.sort(Comparator.comparing(TeamData::getTotalShoot)
                                        .thenComparing(TeamData::getAverageShoot).reversed());
                }else if (teamRankId==6){
                    //球队射正榜
                    allteamdatalist.sort(Comparator.comparing(TeamData::getTotalOntarget)
                                        .thenComparing(TeamData::getAverageOntarget).reversed());
                }else if (teamRankId==7){
                    //球队传球榜
                    allteamdatalist.sort(Comparator.comparing(TeamData::getTotalPass)
                            .thenComparing(TeamData::getTotalOkpass)
                            .thenComparing(TeamData::getAveragePass)
                            .thenComparing(TeamData::getAverageOkpass).reversed());
                }else if (teamRankId==8){
                    //球队越位榜
                    allteamdatalist.sort(Comparator.comparing(TeamData::getTotalOffside)
                                        .thenComparing(TeamData::getAverageOffside).reversed());
                }else if (teamRankId==9){
                    //球队过人榜
                    allteamdatalist.sort(Comparator.comparing(TeamData::getTotalSurpass)
                                        .thenComparing(TeamData::getAverageSurpass).reversed());
                }else if (teamRankId==10){
                    //球队犯规榜
                    allteamdatalist.sort(Comparator.comparing(TeamData::getTotalFoul)
                                        .thenComparing(TeamData::getAverageFoul).reversed());
                }else if (teamRankId==11){
                    //球队黄牌榜
                    allteamdatalist.sort(Comparator.comparing(TeamData::getTotalYcard)
                                        .thenComparing(TeamData::getAverageYcard).reversed());
                }else if (teamRankId==12){
                    //球队红牌榜
                    allteamdatalist.sort(Comparator.comparing(TeamData::getTotalRcard)
                                        .thenComparing(TeamData::getAverageRcard).reversed());
                }
                return Result.success(allteamdatalist);
            }else if(homeAway.equals("主场")){
                if (teamRankId==1){
                    //球队进球榜
                    hometeamdatalist.sort(Comparator.comparing(TeamData::getTotalGoal)
                                         .thenComparing(TeamData::getAverageGoal).reversed());
                }else if(teamRankId==2){
                    //球队失球榜
                    hometeamdatalist.sort(Comparator.comparing(TeamData::getTotalFumble)
                            .thenComparing(TeamData::getAverageFumble).reversed());
                }else if (teamRankId==3){
                    //球队身价榜
                    hometeamdatalist.sort(Comparator.nullsFirst(Comparator.comparing(teamData -> teamData.getTeam().getTeamValue(),(s1,s2)->{
                        return s2.compareTo(s1);
                    })));
                }else if (teamRankId==4){
                    //球队控球榜
                    hometeamdatalist.sort(Comparator.comparing(TeamData::getTotalControl).reversed());
                } else if (teamRankId==5){
                    //球队射门榜
                    hometeamdatalist.sort(Comparator.comparing(TeamData::getTotalShoot)
                            .thenComparing(TeamData::getAverageShoot).reversed());
                }else if (teamRankId==6){
                    //球队射正榜
                    hometeamdatalist.sort(Comparator.comparing(TeamData::getTotalOntarget)
                            .thenComparing(TeamData::getAverageOntarget).reversed());
                }else if (teamRankId==7){
                    //球队传球榜
                    hometeamdatalist.sort(Comparator.comparing(TeamData::getTotalPass)
                            .thenComparing(TeamData::getTotalOkpass)
                            .thenComparing(TeamData::getAveragePass)
                            .thenComparing(TeamData::getAverageOkpass).reversed());
                }else if (teamRankId==8){
                    //球队越位榜
                    hometeamdatalist.sort(Comparator.comparing(TeamData::getTotalOffside)
                            .thenComparing(TeamData::getAverageOffside).reversed() );
                }else if (teamRankId==9){
                    //球队过人榜
                    hometeamdatalist.sort(Comparator.comparing(TeamData::getTotalSurpass)
                            .thenComparing(TeamData::getAverageSurpass).reversed() );
                }else if (teamRankId==10){
                    //球队犯规榜
                    hometeamdatalist.sort(Comparator.comparing(TeamData::getTotalFoul)
                            .thenComparing(TeamData::getAverageFoul).reversed());
                }else if (teamRankId==11){
                    //球队黄牌榜
                    hometeamdatalist.sort(Comparator.comparing(TeamData::getTotalYcard)
                            .thenComparing(TeamData::getAverageYcard).reversed() );
                }else if (teamRankId==12){
                    //球队红牌榜
                    hometeamdatalist.sort(Comparator.comparing(TeamData::getTotalRcard)
                            .thenComparing(TeamData::getAverageRcard).reversed() );
                }
                System.err.println(hometeamdatalist);
                return Result.success(hometeamdatalist);
            }else if (homeAway.equals("客场")){
                if (teamRankId==1){
                    //球队进球榜
                    awayteamdatalist.sort(Comparator.comparing(TeamData::getTotalGoal)
                            .thenComparing(TeamData::getAverageGoal).reversed());
                }else if(teamRankId==2){
                    //球队失球榜
                    awayteamdatalist.sort(Comparator.comparing(TeamData::getTotalFumble)
                            .thenComparing(TeamData::getAverageFumble).reversed());
                }else if (teamRankId==3){
                    //球队身价榜
                    awayteamdatalist.sort(Comparator.nullsFirst(Comparator.comparing(teamData -> teamData.getTeam().getTeamValue(),(s1,s2)->{
                        return s2.compareTo(s1);
                    })));
                }else if (teamRankId==4){
                    //球队控球榜
                    awayteamdatalist.sort(Comparator.comparing(TeamData::getTotalControl).reversed());
                } else if (teamRankId==5){
                    //球队射门榜
                    awayteamdatalist.sort(Comparator.comparing(TeamData::getTotalShoot)
                            .thenComparing(TeamData::getAverageShoot).reversed());
                }else if (teamRankId==6){
                    //球队射正榜
                    awayteamdatalist.sort(Comparator.comparing(TeamData::getTotalOntarget)
                            .thenComparing(TeamData::getAverageOntarget).reversed());
                }else if (teamRankId==7){
                    //球队传球榜
                    awayteamdatalist.sort(Comparator.comparing(TeamData::getTotalPass)
                            .thenComparing(TeamData::getTotalOkpass)
                            .thenComparing(TeamData::getAveragePass)
                            .thenComparing(TeamData::getAverageOkpass).reversed());
                }else if (teamRankId==8){
                    //球队越位榜
                    awayteamdatalist.sort(Comparator.comparing(TeamData::getTotalOffside)
                            .thenComparing(TeamData::getAverageOffside).reversed());
                }else if (teamRankId==9){
                    //球队过人榜
                    awayteamdatalist.sort(Comparator.comparing(TeamData::getTotalSurpass)
                            .thenComparing(TeamData::getAverageSurpass).reversed());
                }else if (teamRankId==10){
                    //球队犯规榜
                    awayteamdatalist.sort(Comparator.comparing(TeamData::getTotalFoul)
                            .thenComparing(TeamData::getAverageFoul).reversed());
                }else if (teamRankId==11){
                    //球队黄牌榜
                    awayteamdatalist.sort(Comparator.comparing(TeamData::getTotalYcard)
                            .thenComparing(TeamData::getAverageYcard).reversed());
                }else if (teamRankId==12){
                    //球队红牌榜
                    awayteamdatalist.sort(Comparator.comparing(TeamData::getTotalRcard)
                            .thenComparing(TeamData::getAverageRcard).reversed());
                }
                System.err.println(awayteamdatalist);
                return Result.success(awayteamdatalist);
            }


        return Result.success(allteamdatalist);
    }

    //前台球员榜单列表加载(以后改造成分页，因为数量比较多，或者就显示前20个)
    @ApiOperation("前台球员榜单列表加载")
    @GetMapping("/front/playerRank")
    public Result playerRank(@RequestParam Integer leagueId,
                             @RequestParam Integer playerRankId) {
        QueryWrapper<PlayerData> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq(leagueId!=null,"league_id",leagueId);

        List<PlayerData> playerDataList = playerDataService.list(queryWrapper);
        //添加场均数据属性字段
        playerDataList.forEach(playerData -> {
            playerData.setPlayer(playerDataService.getplayerinfo(playerData.getPlayerId()));
            playerData.setTeam(teamService.getById(playerData.getTeamId()));
            playerData.setLeague(leagueService.getById(playerData.getLeagueId()));
            playerDataService.countplayerleague(playerData);
        });

        System.err.println(playerDataList);

        if (playerRankId==1){
            //球员射手榜
            playerDataList.sort(Comparator.comparing(PlayerData::getTotalGoal)
                                .thenComparing(PlayerData::getAverageGoal).reversed());
        }else if (playerRankId==2){
            //球员助攻榜
            playerDataList.sort(Comparator.comparing(PlayerData::getTotalAssist)
                                .thenComparing(PlayerData::getAverageAssist).reversed());
        }else if (playerRankId==3){
            //球员身价榜
            playerDataList.sort(Comparator.comparing(playerData ->  playerData.getPlayer().getPlayerValue(),(s1,s2)->{
                return s2.compareTo(s1);
            }));
        }else if (playerRankId==4){
            //球员射门榜
            playerDataList.sort(Comparator.comparing(PlayerData::getTotalShoot)
                                .thenComparing(PlayerData::getAverageShoot).reversed());
        }else if (playerRankId==5){
            //球员射正榜
            playerDataList.sort(Comparator.comparing(PlayerData::getTotalOntarget)
                                .thenComparing(PlayerData::getAverageOntarget).reversed());
        }else if (playerRankId==6){
            //球员传球榜
            playerDataList.sort(Comparator.comparing(PlayerData::getTotalPass)
                                .thenComparing(PlayerData::getTotalOkpass)
                                .thenComparing(PlayerData::getAveragePass)
                                .thenComparing(PlayerData::getAverageOkpass).reversed());
        }else if (playerRankId==7){
            //球员越位榜
            playerDataList.sort(Comparator.comparing(PlayerData::getTotalOffside)
                                .thenComparing(PlayerData::getAverageOffside).reversed());
        }else if (playerRankId==8){
            //球员过人榜
            playerDataList.sort(Comparator.comparing(PlayerData::getTotalSurpass)
                                .thenComparing(PlayerData::getTotalOksurpass)
                                .thenComparing(PlayerData::getAverageSurpass)
                                .thenComparing(PlayerData::getAverageOksurpass).reversed());
        }else if (playerRankId==9){
            //球员抢断榜
            playerDataList.sort(Comparator.comparing(PlayerData::getTotalPreemption)
                                .thenComparing(PlayerData::getAveragePreemption).reversed());
        }else if (playerRankId==10){
            //球员犯规榜
            playerDataList.sort(Comparator.comparing(PlayerData::getTotalFoul)
                                .thenComparing(PlayerData::getAverageFoul).reversed());
        }else if (playerRankId==11){
            //球员被犯规榜
            playerDataList.sort(Comparator.comparing(PlayerData::getTotalBefouled)
                                .thenComparing(PlayerData::getAverageBefouled).reversed());
        }else if (playerRankId==12){
            //球员黄牌榜
            playerDataList.sort(Comparator.comparing(PlayerData::getTotalYcard)
                                .thenComparing(PlayerData::getAverageYcard).reversed());
        }else if (playerRankId==13){
            //球员红牌榜
            playerDataList.sort(Comparator.comparing(PlayerData::getTotalRcard)
                                .thenComparing(PlayerData::getAverageRcard).reversed());
        }

        return Result.success(playerDataList);
    }


    //前台球队能力排行
    @ApiOperation("前台球队能力排行")
    @GetMapping("/front/RankTeamAbility")
    public Result RankTeamAbility(@RequestParam Integer leagueId,@RequestParam String teamAbilityId){
        List<Team> teamList = new ArrayList<>();
        if (leagueId!=null){//根据league查找team
            teamList=leagueService.findTeamByLeagueId(leagueId);
        }else{//查询全部球队
            teamList=teamService.list();
        }

        List<TeamAbility> teamAbilityList=new ArrayList<>();
        teamList.forEach(team -> {
            QueryWrapper<TeamAbility> queryWrapperta=new QueryWrapper<>();
            queryWrapperta.eq("team_id",team.getId());
            team.setTeamAbility(teamAbilityService.getOne(queryWrapperta));
            teamAbilityList.add(team.getTeamAbility());
        });
        teamAbilityList.forEach(teamAbility -> {
            teamAbility.setTeam(teamService.getById(teamAbility.getTeamId()));
            System.err.println(teamAbilityList);
        });
        System.err.println(teamAbilityList);
        if (teamAbilityId.equals("1") || teamAbilityId.equals("")){//球队综合能力
            teamAbilityList.sort(Comparator.comparing(TeamAbility::getCapability));
        }else if(teamAbilityId.equals("2")){//球队进攻能力
            teamAbilityList.sort(Comparator.comparing(TeamAbility::getAttack));
        }else if (teamAbilityId.equals("3")){//球队防守能力
            teamAbilityList.sort(Comparator.comparing(TeamAbility::getDefense));
        }

        return Result.success(teamAbilityList);
    }



    //前台球员能力排行
    @ApiOperation("前台球队能力排行")
    @GetMapping("/front/RankPlayerAbility")
    public Result RankPlayerAbility(@RequestParam Integer league1Id,
                                    @RequestParam Integer teamId,
                                    @RequestParam String playerAbilityId){

        List<PlayerAbility> playerAbilitieList = new ArrayList<>();
        if (league1Id!=null){
            if (teamId!=null){//有联赛有球队查询
                    QueryWrapper<Player> queryWrapperp = new QueryWrapper<>();
                    queryWrapperp.eq("team_id",teamId);
                    List<Player> playerList=playerService.list(queryWrapperp);
                    playerList.forEach(player -> {
                        QueryWrapper<PlayerAbility> queryWrapperpa= new QueryWrapper<>();
                        queryWrapperpa.eq("player_id",player.getId());
                        player.setPlayerAbility(playerAbilityService.getOne(queryWrapperpa));
                        playerAbilitieList.add(player.getPlayerAbility());
                    });
                    playerAbilitieList.forEach(playerAbility -> {
                        playerAbility.setPlayer(playerService.getById(playerAbility.getPlayerId()));
                        System.err.println("有联赛有球队查询");
                        System.err.println(playerAbilitieList);
                    });
            }else{//有联赛无球队查询
                List<Team> teamList = leagueService.findTeamByLeagueId(league1Id);
                teamList.forEach(team -> {
                    QueryWrapper<Player> queryWrapperp = new QueryWrapper<>();
                    queryWrapperp.eq("team_id",team.getId());
                    List<Player> playerList=playerService.list(queryWrapperp);
                    playerList.forEach(player -> {
                        QueryWrapper<PlayerAbility> queryWrapperpa= new QueryWrapper<>();
                        queryWrapperpa.eq("player_id",player.getId());
                        player.setPlayerAbility(playerAbilityService.getOne(queryWrapperpa));
                        playerAbilitieList.add(player.getPlayerAbility());
                    });
                    playerAbilitieList.forEach(playerAbility -> {
                        playerAbility.setPlayer(playerService.getById(playerAbility.getPlayerId()));
                        System.err.println("有联赛无球队查询");
                        System.err.println(playerAbilitieList);
                    });
                });
            }
        }else{
            if (teamId!=null){//无联赛有球队查询
                    QueryWrapper<Player> queryWrapperp = new QueryWrapper<>();
                    queryWrapperp.eq("team_id",teamId);
                    List<Player> playerList=playerService.list(queryWrapperp);
                    playerList.forEach(player -> {
                        QueryWrapper<PlayerAbility> queryWrapperpa= new QueryWrapper<>();
                        queryWrapperpa.eq("player_id",player.getId());
                        player.setPlayerAbility(playerAbilityService.getOne(queryWrapperpa));
                        playerAbilitieList.add(player.getPlayerAbility());
                    });
                    playerAbilitieList.forEach(playerAbility -> {
                        playerAbility.setPlayer(playerService.getById(playerAbility.getPlayerId()));
                        System.err.println("无联赛有球队查询");
                        System.err.println(playerAbilitieList);
                    });
            }else{//无联赛无球队查询
                List<Team> teamList = teamService.list();
                teamList.forEach(team -> {
                    QueryWrapper<Player> queryWrapperp = new QueryWrapper<>();
                    queryWrapperp.eq("team_id",team.getId());
                    List<Player> playerList=playerService.list(queryWrapperp);
                    playerList.forEach(player -> {
                        QueryWrapper<PlayerAbility> queryWrapperpa= new QueryWrapper<>();
                        queryWrapperpa.eq("player_id",player.getId());
                        player.setPlayerAbility(playerAbilityService.getOne(queryWrapperpa));
                        playerAbilitieList.add(player.getPlayerAbility());
                    });
                    playerAbilitieList.forEach(playerAbility -> {
                        playerAbility.setPlayer(playerService.getById(playerAbility.getPlayerId()));
                        System.err.println("无联赛无球队查询");
                        System.err.println(playerAbilitieList);
                    });
                });
            }
        }

        if (playerAbilityId.equals("1") || playerAbilityId.equals("")){
            playerAbilitieList.sort(Comparator.comparing(PlayerAbility::getCapability));
        }else if (playerAbilityId.equals("2")){
            playerAbilitieList.sort(Comparator.comparing(PlayerAbility::getAttack));
        }else if (playerAbilityId.equals("3")){
            playerAbilitieList.sort(Comparator.comparing(PlayerAbility::getDefense));
        }else if (playerAbilityId.equals("4")){
            playerAbilitieList.sort(Comparator.comparing(PlayerAbility::getSpeed));
        }else if (playerAbilityId.equals("5")){
            playerAbilitieList.sort(Comparator.comparing(PlayerAbility::getPower));
        }else if (playerAbilityId.equals("6")){
            playerAbilitieList.sort(Comparator.comparing(PlayerAbility::getShoot));
        }else if (playerAbilityId.equals("7")){
            playerAbilitieList.sort(Comparator.comparing(PlayerAbility::getPass));
        }else if (playerAbilityId.equals("8")){
            playerAbilitieList.sort(Comparator.comparing(PlayerAbility::getDribble));
        }else if (playerAbilityId.equals("9")){
            playerAbilitieList.sort(Comparator.comparing(PlayerAbility::getTechnique));
        }else if (playerAbilityId.equals("10")){
            playerAbilitieList.sort(Comparator.comparing(PlayerAbility::getFreekick));
        }else if (playerAbilityId.equals("11")){
            playerAbilitieList.sort(Comparator.comparing(PlayerAbility::getStamina));
        }else if (playerAbilityId.equals("12")){
            playerAbilitieList.sort(Comparator.comparing(PlayerAbility::getHeader));
        }else if (playerAbilityId.equals("13")){
            playerAbilitieList.sort(Comparator.comparing(PlayerAbility::getJump));
        }else if (playerAbilityId.equals("14")){
            playerAbilitieList.sort(Comparator.comparing(PlayerAbility::getKeeper));
        }else if (playerAbilityId.equals("15")){
            playerAbilitieList.sort(Comparator.comparing(PlayerAbility::getTeamwork));
        }else if (playerAbilityId.equals("16")){
            playerAbilitieList.sort(Comparator.comparing(playerAbility -> playerAbility.getPlayer().getPlayerValue()));
        }


        return Result.success(playerAbilitieList);
    }


}
